/*
 *
 */
package org.jiscinvolve.astrodabis.barberry.common.data;

import java.net.URI;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import static org.junit.Assert.*;

/*
 *
 */
public class TapRowTestCase
extends DataTestBase
    {

    protected TapService service ;
    protected TapTable   table   ;
    protected TapColumn  column  ;

    @Before
    public void before()
        {
        super.before();
        log.debug("TapRowTestCase.before()");

        service = womble().services().create(
            unique(
                URI.create(
                    "ivo://org.astrodabis.test/service/"
                    )
                ),
            "test-service"
            );
        table = service.tables().create(
            unique(
                "test-table"
                )
            );
        column = table.columns().create(
            unique(
                "test-column"
                )
            );
        }

    @Test
    public void checkCreated()
        {
        compare(
            column.rows().create(
                unique(
                    "test-row"
                    )
                ),
            column
            );
        }    

    @Test
    public void checkSelectFromColumn()
        {
        assertNotNull(
            column.rows().select()
            );
        assertNotNull(
            column.rows().select().iterator()
            );
        assertFalse(
            column.rows().select().iterator().hasNext()
            );
        assertNotNull(
            column.rows().create(
                unique(
                    "test-row"
                    )
                )
            );
        assertNotNull(
            column.rows().select()
            );
        assertNotNull(
            column.rows().select().iterator()
            );
        assertTrue(
            column.rows().select().iterator().hasNext()
            );
        compare(
            column.rows().select().iterator().next(),
            column
            );
        }    

    @Test
    public void checkSelectFromColumnCount()
        {
        for (int count = 1 ; count <= 100 ; count++)
            {
            column.rows().create(
                unique(
                    "test-row"
                    )
                );
            assertEquals(
                count,
                count(
                    column.rows().select()
                    )
                );
            }    
        }    

    @Test
    public void checkSelectFromColumnCountPlusOne()
        {
        for (int count = 1 ; count <= 100 ; count++)
            {
            column.rows().create(
                "test-row"
                );
            column.rows().create(
                unique(
                    "test-row"
                    )
                );
            assertEquals(
                (count + 1),
                count(
                    column.rows().select()
                    )
                );
            }    
        }    

    @Test
    public void checkSelectFromFactoryByIdent()
        {
        compare(
            womble().rows().select(
                column.rows().create(
                    unique(
                        "test-row"
                        )
                    ).ident()
                ),
            column
            );
        }    

    @Test
    public void checkSelectFromColumnByValue()
        {
        compare(
            column.rows().select(
                column.rows().create(
                    unique(
                        "test-row"
                        )
                    ).value()
                ),
            column
            );
        }    
    }

